﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Interface;
using Spire.Doc.Reporting;

namespace DocX_WordDemo
{
    public static class MailMerge
    {
        public static int lastIndex = 0;

        public static void DoMailMerge(string fileName, string outputFileName)
        {
            Document doc = new Document(fileName);
            string[] fieldNames = { "Contact Name", "Fax", "Date" };
            string[] fieldValues = { "Titan.Net", "+1 (69) 123456", DateTime.Now.ToString() };
            doc.MailMerge.Execute(fieldNames, fieldValues);
            doc.SaveToFile(outputFileName);
        }

        public static void DoMailMergeEvent(string fileName, string outputFileName)
        {
            Document doc = new Document(fileName);
            List<CustomRecord> records = new List<CustomRecord>
            {
                new CustomRecord{ContactName="Lily",Fax="786-324-10",Date=DateTime.Now},
                new CustomRecord{ContactName="Lucy",Fax="779-138-13",Date=DateTime.Now},
                new CustomRecord{ContactName="James",Fax="363-287-02",Date=DateTime.Now}
            };
            doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField);
            doc.MailMerge.ExecuteGroup(new MailMergeDataTable("Customer", records));
            doc.SaveToFile(outputFileName);
        }

        public static void MailMerge_MergeField(object sender, MergeFieldEventArgs args)
        {
            if (args.RowIndex > lastIndex)
            {
                lastIndex = args.RowIndex;
                AddPageBreakForMergeField(args.CurrentMergeField);
            }
        }

        public static void AddPageBreakForMergeField(IMergeField mergeField)
        {
            bool foundGroupStart = false;
            Paragraph paramgraph = mergeField.PreviousSibling.Owner as Paragraph;
            MergeField merageField = null;
            while (!foundGroupStart)
            {
                paramgraph = paramgraph.PreviousSibling as Paragraph;
                for (int i = 0; i < paramgraph.Items.Count; i++)
                {
                    merageField = paramgraph.Items[i] as MergeField;
                    if ((merageField != null) && (merageField.Prefix == "GroupStart"))
                    {
                        foundGroupStart = true;
                        break;
                    }
                }
            }
            paramgraph.AppendBreak(BreakType.PageBreak);
        }

        public static void DoNestedMailMerge(string fileName, string outputFileName)
        {
            DataSet ds = new DataSet();
            ds.ReadXml("../../Document/Orders.xml");
            Document doc = new Document(fileName);
            List<DictionaryEntry> list = new List<DictionaryEntry>
            {
                new DictionaryEntry{Key="Customer",Value=string.Empty},
                new DictionaryEntry{Key="Order",Value="Customer_Id = %Customer.Customer_Id%"}
            };
            doc.MailMerge.ExecuteWidthNestedRegion(ds, list);
            doc.SaveToFile(outputFileName);
        }

    }

    class CustomRecord
    {
        public string ContactName { get; set; }
        public string Fax { get; set; }
        public DateTime Date { get; set; }

        public CustomRecord() { }
    }
}
